package com.skit.mine.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.skit.common.base.BaseActivity;
import com.skit.common.bean.StatusResult;
import com.skit.common.config.RetrofitClient;
import com.skit.common.http.HttpUtils;
import com.skit.common.utils.ToastUtils;
import com.skit.mine.LoginActivity;
import com.skit.mine.R;
import com.skit.mine.RegisterViewHelper;
import com.skit.mine.databinding.UserRegisterCompleteBinding;
import com.skit.common.retrofit.UserProtocol;

import retrofit2.Call;


public class CompleteInfoActivity extends BaseActivity {
    UserRegisterCompleteBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.user_register_complete);
        RegisterViewHelper registerViewHelper = new ViewModelProvider(this).get(RegisterViewHelper.class);
        binding.setRegisterViewHelper(registerViewHelper);
        binding.setLifecycleOwner(this);

        binding.toolbar.setNavigationIcon(getDrawable(R.drawable.ic_keyboard_backspace_black_24dp));
        setSupportActionBar(binding.toolbar);
        if (getSupportActionBar() != null) {
//            getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setTitle("");

        String email = getIntent().getStringExtra("email");
        String pwd = getIntent().getStringExtra("pwd");

        binding.setDoneListener(v -> {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("正在请求");
            progressDialog.setCancelable(false);
            progressDialog.show();
            UserProtocol userProtocol = RetrofitClient.getService(UserProtocol.class);
            Call<StatusResult<String>> register = userProtocol.register(email, binding.userNameEditText.getText().toString(), pwd, binding.userStuidEditText.getText().toString());
            HttpUtils.doRequest(register, result -> {
                progressDialog.dismiss();
                if (result == null) {
                    ToastUtils.showToast("请求失败");
                    return;
                }
                if (result.isSuccess()) {
                    Intent intent = new Intent(CompleteInfoActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtils.showToast(result.getMsg());
                }
            });
        });

    }


    public static void startActivity(Context context, String email, String pwd) {
        Intent intent = new Intent(context, CompleteInfoActivity.class);
        intent.putExtra("email", email);
        intent.putExtra("pwd", pwd);
        context.startActivity(intent);
    }
}
